package com.xiaoyilin.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HeadrsController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/headers/MatchNo/send")
    public void headerSendMatchNo() {
        MessageProperties messageProperties = new MessageProperties();
        //设置消息是否持久化。Persistent表示持久化，Non-persistent表示不持久化
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        messageProperties.setContentType("UTF-8");
        //没有匹配
        messageProperties.setHeader("Three", "C");

        Message message = new Message("header-queue-msg-no-match".getBytes(), messageProperties);

        rabbitTemplate.convertAndSend("my.headers.exchange", null, message);
    }


    @GetMapping("/headers/matchAll/send")
    public void headerSendMatchAll() {
        Map<String, Object> map_all = new HashMap<>(2);
        map_all.put("One", "A");
        map_all.put("Two", "B");

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        messageProperties.setContentType("UTF-8");
        messageProperties.getHeaders().putAll(map_all);

        Message message = new Message("header-queue-msg-match-all".getBytes(), messageProperties);

        rabbitTemplate.convertAndSend("my.headers.exchange", null, message);

    }


    @GetMapping("/headers/matchAnysend")
    public void headerSendMatchAny() {
        Map<String, Object> map_any = new HashMap<>(1);
        map_any.put("One", "A");
        //map_any.put("Two", "B");
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        messageProperties.setContentType("UTF-8");
        messageProperties.getHeaders().putAll(map_any);

        Message message = new Message("header-queue-msg-match-any".getBytes(), messageProperties);

        rabbitTemplate.convertAndSend("my.headers.exchange", null, message);

    }


}
